package com.huike.business.service.impl;



import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessTrackRecordMapper recordMapper;
    @Autowired
    private TbBusinessMapper businessMapper;
    @Autowired
    private ISysDictDataService sysDictDataService;


    @Override
    public String insertTbBusinessTrackRecord(BusinessTrackVo businessTrackVo) {
        //1.数据拷贝
        TbBusinessTrackRecord trackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo, trackRecord);
        trackRecord.setCreateTime(DateUtils.getNowDate());
        trackRecord.setCreateBy(SecurityUtils.getUsername());
        TbBusiness business = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo, business);
        business.setStatus(TbBusiness.StatusType.FOLLOWING.getValue());
        business.setId(businessTrackVo.getBusinessId());
        //2.修改商机表tb_business
        businessMapper.updateTbBusiness(business);
        //3.新增商机跟进表tb_business_track_record
        return recordMapper.addTbBusinessTrackeRecord(trackRecord);
    }

    @Override
    public List<TbBusinessTrackRecord> getTbBusinessTrackeRecordList(Long businessId) {
        //1.获取所有的跟进列表，但是每个实体中的key_items沟通重点都是数字，我们要查数据字典表，然后获取值
        List<TbBusinessTrackRecord> tbBusinessTrackeRecordList = recordMapper.getTbBusinessTrackeRecordList(businessId);
        //2.遍历跟进列表
        for (TbBusinessTrackRecord tbBusinessTrackRecord :
                tbBusinessTrackeRecordList) {
            //3获取每个key_items
            String keyItems = tbBusinessTrackRecord.getKeyItems();
            //4.根据keyItems查询数据字典表sys_dict_data, communication_point是字典表中
            String dictLabel =
                    sysDictDataService.selectDictLabel("communication_point", keyItems);
            //5.存储到list集合中
            tbBusinessTrackRecord.getKeys().add(dictLabel);
        }
        return tbBusinessTrackeRecordList;
    }
}
